﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class WakeUpConfigStartPos :WakeUpConfigBase{
    public Vector2 StartPos;
    public WakeUpConfigStartPos(Vector2 pos) { StartPos = pos; }
}

public class WakeUpConfigStartPosAndOri : WakeUpConfigStartPos {
    public Vector2 Ori;
    public WakeUpConfigStartPosAndOri(Vector2 pos,Vector2 ori):base(pos) {
        this.Ori = ori;
    }
}

public class WakeUpConfigBulletShell : WakeUpConfigStartPosAndOri {
    public float AngleSpeed;
    public WakeUpConfigBulletShell(Vector2 pos, Vector2 ori,float angleSpeed):base(pos,ori) {
        this.AngleSpeed = angleSpeed;
    }
}

public class BulletShell : PrefabCommonBase
{
    public float BulletShellSpeedValue = 2;
    public float BulletAngleSpeedValue = 1000;
    public float BulletPhysicDeadTime = 3;
    public float BulletImageDeadTime = 6;
    public override void SetStatusByConfig(WakeUpConfigBase config)
    {
        WakeUpConfigBulletShell wc = config as WakeUpConfigBulletShell;
        if (wc != null) {
            this.transform.localPosition = new Vector3(wc.StartPos.x, wc.StartPos.y, this.transform.localPosition.z);
            var rig2d = this.GetComponent<Rigidbody2D>();
            if (rig2d != null)
            {
                wc.Ori = wc.Ori.normalized;
                //Debug.Log("bori:"+wc.Ori.x+" y:"+wc.Ori.y);
                rig2d.velocity = new Vector2(
                    wc.Ori.x * this.BulletShellSpeedValue,
                    wc.Ori.y * this.BulletShellSpeedValue
                    );
                rig2d.angularVelocity = wc.AngleSpeed;
            }
        }
        
    }

    protected override void onActive()
    {
        this.gameObject.SetActive(true);
        this.GetComponent<Rigidbody2D>().WakeUp();

        this.CancelInvoke();
        this.Invoke("onPhysicNeedDead", this.BulletPhysicDeadTime);
        this.Invoke("onImageNeedDead", this.BulletImageDeadTime);
    }

    protected override void onIdle()
    {
        this.GetComponent<Rigidbody2D>().Sleep();
    }

    protected override void onSleep()
    {
        this.gameObject.SetActive(false);
    }


    private void onPhysicNeedDead() {
        this.State = EnumItemState.E_IDLE;
    }
    private void onImageNeedDead() {
        this.State = EnumItemState.E_SLEEP;
    }
}
